package com.bw.user_module.login

import android.os.Bundle
import android.text.TextUtils
import androidx.activity.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.user_module.databinding.ActivityLoginBinding
import com.kf.common.mvi.view.BaseActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@Route(path = "/user/loginactivity")
class LoginActivity : BaseActivity<ActivityLoginBinding, LoginViewModel>() {
    override fun createViewModel(): LoginViewModel {
        return viewModels<LoginViewModel>().value
    }

    override fun createViewBind(): ActivityLoginBinding {
        return ActivityLoginBinding.inflate(inflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        initView()

        bindUiState()

        mView.login.setOnClickListener {
            var username = mView.loginUsername.text.toString()
            var password = mView.loginPassword.text.toString()

            if (TextUtils.isEmpty(username)) {
                ToastUtils.showShort("用户名不能为空")
                return@setOnClickListener
            }

            if (TextUtils.isEmpty(password)) {
                ToastUtils.showShort("密码不能为空")
                return@setOnClickListener
            }

            var map: HashMap<String, Any> = HashMap()
            map.put("time", System.currentTimeMillis())
            map.put("uName", username)
            map.put("uPwd", password)
            mViewModel.requestLogin(map)
        }
    }

    private fun initView() {
        var userName = SPUtils.getInstance().getString("userName", "hxw1")
        var passWord = SPUtils.getInstance().getString("passWord", "123456")
        if (!TextUtils.isEmpty(userName)) {
            mView.loginUsername.setText(userName)
        }
        if (!TextUtils.isEmpty(passWord)) {
            mView.loginPassword.setText(passWord)
        }
    }

    private fun bindUiState() {
        lifecycleScope.launch(Dispatchers.Main) {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                mViewModel.mutableContainer.uiStateFlow.collect {
                    LogUtils.e(it.entity.values.toString())
                    if (!it.entity.values!!.homeURL.equals("")) {
                        SPUtils.getInstance().put("token", it!!.entity.values!!.token)
                        SPUtils.getInstance().put("userName", it!!.entity.values!!.uName)
                        SPUtils.getInstance().put("dept", it!!.entity.values!!.dept_id)
                        SPUtils.getInstance().put("passWord", it!!.entity.values!!.uPwd)
                        SPUtils.getInstance().put("userId", it!!.entity.values!!.uId)
                        ARouter.getInstance().build("/main/mainactivity")
                            .withString("loadUrl", it.entity!!.values!!.homeURL).navigation()
                    }

                }
            }
        }
    }
}